package net.love5z.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.love5z.web.common.cache.GaeCacheService;
import net.love5z.web.common.utils.ObjectPropUtils;
import net.love5z.web.common.utils.ServletUtils;
import net.love5z.web.controller.model.User;
import net.love5z.web.controller.model.UserPacRule;
import net.love5z.web.service.PacGeneratorService;
import net.love5z.web.service.UserPacRuleService;
import net.love5z.web.service.bo.UserPacRuleBo;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class PacController {

    @Autowired
    private PacGeneratorService pacGeneratorService;

    @Autowired
    private UserPacRuleService  userPacRuleService;

    @Autowired
    private GaeCacheService     cacheService;

    @RequestMapping(value = "/pac/", method = RequestMethod.GET)
    public String index(ModelMap map, HttpServletRequest request, HttpServletResponse response) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("SESSION_LOGIN_USER");

        List<UserPacRuleBo> _userPacRuleList = userPacRuleService.getUserPacRuleListByUserId(user
            .getId());
        List<UserPacRule> userPacRuleList = new ArrayList<UserPacRule>();
        ObjectPropUtils.copyCollectionProperties(_userPacRuleList, userPacRuleList,
            UserPacRule.class);

        map.put("user_pac_rule_list", userPacRuleList);

        return "pac/index.vm";
    }

    @RequestMapping(value = "/pac/add_pac_rule", method = RequestMethod.POST)
    public String add(ModelMap map, HttpServletRequest request, HttpServletResponse response) {

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("SESSION_LOGIN_USER");

        String rule = request.getParameter("pac_rule");

        if (StringUtils.isNotBlank(rule)) {
            userPacRuleService.addUserPacRuleByUserId(rule, user.getId());
        }

        return "redirect:" + ServletUtils.getBaseUrl(request) + "/pac/";
    }

    @RequestMapping(value = "/userpac/common.pac", method = RequestMethod.GET)
    public String common(ModelMap map) {
        String pacText = null;
        pacText = pacGeneratorService.getPac(null, "localhost", 7070);

        map.put("pacText", pacText);
        return "pac/pac.vm";
    }

    @RequestMapping(value = "/userpac/{userId}.pac", method = RequestMethod.GET)
    public String userPac(ModelMap map, @PathVariable("userId")
    long userId) {
        String pacText = null;

        String cacheKey = "love5z.pac.userpac." + userId;

        pacText = (String) cacheService.get(cacheKey);

        if (StringUtils.isBlank(pacText)) {
            pacText = pacGeneratorService.getPac(generateUserRule(userId), "localhost", 7070);
            if (StringUtils.isNotBlank(pacText)) {
                cacheService.put(cacheKey, pacText, 2 * 60 * 60);
            }
        }

        map.put("pacText", pacText);
        return "pac/pac.vm";
    }

    private String generateUserRule(long userId) {
        List<String> userRules = new ArrayList<String>();

        List<UserPacRuleBo> userPacRuleBoList = userPacRuleService
            .getUserPacRuleListByUserId(userId);

        for (UserPacRuleBo userPacRuleBo : userPacRuleBoList) {
            userRules.add(userPacRuleBo.getRule());
        }

        StringBuilder sb = new StringBuilder();
        for (String userRule : userRules) {
            sb.append(userRule).append("\n");
        }

        return sb.toString();
    }
}
